package com.esp.soccertutor.uc;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import com.esp.soccertutor.R;
import com.esp.soccertutor.android.fbreader.FBReader;
import com.esp.soccertutor.android.fbreader.TOCActivity;
import com.esp.soccertutor.fbreader.fbreader.FBReaderApp;
import com.esp.soccertutor.fbreader.library.Bookmark;
import com.esp.soccertutor.util.Const;
import com.esp.soccertutor.zlibrary.core.application.ZLApplication;
import com.esp.soccertutor.zlibrary.text.view.ZLTextView;

public class CustomMenu_TopReading {

	
	
	private Context mContext = null;
	private LayoutInflater mLayoutInflater = null;
	private PopupWindow mPopupWindow = null;
	private boolean mIsShowing = false;
	
	private TextView txtBookName;
	
	private TextView _tv_PageValue;
	
	private SeekBar ReadBook;
	private View mView;
	
	private RelativeLayout frameMain;
	
	private Activity objActivity;
	
	public boolean isShowing() { return mIsShowing; }
	
	public CustomMenu_TopReading(Context context, LayoutInflater lo) {
		mContext = context;
		mLayoutInflater = lo;
		
		mView= mLayoutInflater.inflate(R.layout.custom_menu_top_for_reading, null);
		frameMain = (RelativeLayout) mView.findViewById(R.id.MenuFrame);
		txtBookName = (TextView) mView.findViewById(R.id.txtBookName);
		txtBookName.setTypeface(Const.tf);
		
		_tv_PageValue=(TextView) mView.findViewById(R.id._tv_PageValue);
		ReadBook = (SeekBar) mView.findViewById(R.id.ReadBook);
		ReadBook.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			

			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			
			public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
				System.out.println("**********************************");
				final int page = progress + 1;
				final int pagesNumber = seekBar.getMax() + 1;
				_tv_PageValue.setText(makeProgressText(page,pagesNumber));
				gotoPage(page);
				
			}
		});
	}

	public void setActivity(Activity objActivity)
	{
		this.objActivity = objActivity;
	}
	public Activity getActivity()
	{
		return this.objActivity;
	}
	
	private void gotoPage(int page) {
		
		FBReaderApp fbReader = (FBReaderApp)ZLApplication.Instance();
		final ZLTextView view = fbReader.getTextView();
		if (page == 1) {
			view.gotoHome();
		} else {
			view.gotoPage(page);
		}
		fbReader.getViewWidget().reset();
		fbReader.getViewWidget().repaint();
	}
	private static String makeProgressText(int page, int pagesNumber) {
		return page + " / " + pagesNumber;
	}
	private void setupNavigation() {
		
		final FBReaderApp fbReader = (FBReaderApp)ZLApplication.Instance();
		final SeekBar slider = (SeekBar)mView.findViewById(R.id.ReadBook);
		final TextView text = (TextView)mView.findViewById(R.id._tv_PageValue);

		final ZLTextView textView = fbReader.getTextView();
		final int page = textView.computeCurrentPage();
		final int pagesNumber = textView.computePageNumber();

		if (slider.getMax() != pagesNumber - 1 || slider.getProgress() != page - 1) {
			slider.setMax(pagesNumber - 1);
			slider.setProgress(page - 1);
			text.setText(makeProgressText(page, pagesNumber));
		}
	}

	public void setBackGround(int Resource)
	{
		frameMain.setBackgroundResource(Resource);
	}
	
	public void setBooName(String BookName)
	{
		txtBookName.setText(BookName);
	}
	
	public void setProgress(int progress)
	{
		ReadBook.setProgress(progress);
	}
	
	public void setMax(int max)
	{
		ReadBook.setMax(max);
	}
	
	public int getProgress()
	{
		return ReadBook.getProgress();
	}
	
	public int getMax()
	{
		return ReadBook.getMax();
	}
	
	public synchronized void show(View v) {
		
		setupNavigation();
		mIsShowing = true;
		if (mPopupWindow != null) return; //already showing
		
		Display display = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
//		View mView= mLayoutInflater.inflate(R.layout.custom_menu_top_for_reading, null);
//		txtBookName = (TextView) mView.findViewById(R.id.txtBookName);
		
		int[] btn = new int[]{R.id.btnBack, R.id.btnInfo};
		for(int ID : btn)
		{
			Button bt = (Button) mView.findViewById(ID);
			bt.setOnClickListener(new OnClickListener() { 
				
				public void onClick(View v) {
					Intent intent = null;
					switch (v.getId()) {
						case R.id.btnBack:
							//intent = new Intent(mContext, BookListAll.class);
							
							final FBReaderApp fbReader = (FBReaderApp)ZLApplication.Instance();
							Bookmark bookmark = fbReader.addBookmark(20,true);
							bookmark.save();
							Toast.makeText(Const.CONTEXT,"Bookmark done",Toast.LENGTH_LONG).show();							
							break;
						case R.id.btnInfo:
							
							//intent = new Intent(mContext, BookInfo.class);
							intent = new Intent(mContext, TOCActivity.class);
							hide();
							FBReader.BottomMenu.hide();
							
							mContext.startActivity(intent);
							//objActivity.finish();
							break;
				
						default:
							break;
					}
					/*hide();
					FBReader.BottomMenu.hide();
					mContext.startActivity(intent);
					objActivity.finish();*/
				}
			});
		}
		
		mPopupWindow = new PopupWindow(mView,android.view.ViewGroup.LayoutParams.FILL_PARENT, 150, false);
        mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
        mPopupWindow.setWidth(display.getWidth());
//        mPopupWindow.showAtLocation(v, Gravity.TOP, 0, v.getHeight());
        mPopupWindow.showAtLocation(v, Gravity.TOP, 0, 0);
	}
	
	public synchronized void hide() {
		mIsShowing = false;
		if (mPopupWindow != null) {
			mPopupWindow.dismiss();
			mPopupWindow = null;
		}
		return;
	}
}
